{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-01-13T17:10:56.412591400Z",
     "start_time": "2024-01-13T17:10:56.388242400Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['我', '爱', '北京', '天安门']\n",
      "我/ 爱/ 北京/ 天安门\n",
      "我 爱 北京 天安门 "
     ]
    }
   ],
   "source": [
    "import jieba\n",
    "\n",
    "text = \"我爱北京天安门\"\n",
    "seg_list = jieba.lcut(text)  # 使用lcut返回列表形式的结果\n",
    "print(seg_list)\n",
    "print(\"/ \".join(seg_list))  # 输出：\"我/ 爱/ 北京/ 天安门\"\n",
    "\n",
    "# 或者直接输出分词结果\n",
    "for word in jieba.cut(text):\n",
    "    print(word, end=' ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我/ 爱/ 北京/ 天安/ 天安门\n"
     ]
    }
   ],
   "source": [
    "seg_list = jieba.lcut_for_search(text)\n",
    "print(\"/ \".join(seg_list))  # 输出：\"我/ 爱/ 北京/ 天安/ 天安门\""
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-13T17:11:34.224295500Z",
     "start_time": "2024-01-13T17:11:34.171321100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我(r)\n",
      "爱(v)\n",
      "北京(ns)\n",
      "天安门(ns)\n"
     ]
    }
   ],
   "source": [
    "from jieba import posseg as pseg\n",
    "\n",
    "words = pseg.cut(text)\n",
    "for word, flag in words:\n",
    "    print(f\"{word}({flag})\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-13T17:12:44.648521500Z",
     "start_time": "2024-01-13T17:12:44.339751100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "关键词: 天安门, 权重: 4.49770143677\n",
      "关键词: 北京, 权重: 2.33370115436\n"
     ]
    }
   ],
   "source": [
    "import jieba.analyse\n",
    "\n",
    "keywords = jieba.analyse.extract_tags(text, topK=5, withWeight=True)\n",
    "for item in keywords:\n",
    "    print(f\"关键词: {item[0]}, 权重: {item[1]}\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-13T17:13:36.254582300Z",
     "start_time": "2024-01-13T17:13:35.953013500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
